// pages/hotSeach/hotSeach.js
var app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: app.globalData.imageUrl,
    userInfo: '',
    actiItem: '',
    focus:false,
    seachValue:'',
    search: { datagrid:''},
    page: 1,
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    if(options.type == 0){
      wx.setNavigationBarTitle({ title: '热门' });
      this.getbusinesshotList()
      this.data.search.isHot = 1
    }
    else if(options.type == 1){
      wx.setNavigationBarTitle({ title: '新入' });
      this.getbusinessnewList()
      this.data.search.isNew = 1
    }
    else{
      wx.setNavigationBarTitle({ title: options.title });
      if(options.id == 'null'){
        this.getbusinessAllList()
      }
      else {
        this.getShopType(options.id);
        this.data.search.businessType = options.id
      }  
    }
  },
  // 打开商家页面
  openIndex(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/shopIndex/shopIndex?id=" + id
    })
  },
  // 打电话
  toggleSelect: function (e) {
    var show = e.currentTarget.dataset.value == "true" ? true : false;
    this.setData({
      showSelect: show,
      current_tel: e.currentTarget.dataset.tel
    })
  },
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.value
    })
    this.setData({
      showSelect: false
    })
  },
  // 查询所有商铺
  getbusinessAllList: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopInfo/all',
      data: {
        // currentPage: this.data.page,
        // pageNumber: '10'
        status: 2
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        _this.setData({
          loadSuccess: true
        })
        wx.hideLoading()
        if(res.data.success){
          _this.setData({
            actiItem: res.data.data
          })
        }
      }
    })
  },
  // 商铺类型查询
  getShopType: function(id){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopInfo/all',
      data: {
        currentPage: 1,
        pageNumber: 15,
        industry: id,
        status: 2
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        _this.setData({
          loadSuccess: true,
          actiItem: res.data.data
        })
        wx.hideLoading()
      }
    })
  },
  // 商家店铺列表热门
  getbusinesshotList: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopInfo/getHotShopInfo',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        _this.setData({
          loadSuccess: true,
          actiItem: res.data.data
        })
        wx.hideLoading()
      }
    })
  },
  // 商家店铺列表新入
  getbusinessnewList: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopInfo/all',
      data: {
        // currentPage: this.data.page,
        // pageNumber: '10',
        status:2
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        _this.setData({
          loadSuccess: true,
          actiItem: res.data.data
        })
        wx.hideLoading()
      }
    })
  },
  addHistory: function (value) {
    wx.request({
      url: app.globalData.httpApiCar + 'searchInfo/save',
      data: {
        userId: this.data.userInfo.id,
        serachName: value,
        type: 4,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
      }
    })
  },
  inputFocus:function(e){
    this.setData({
      focus:true
    })
  },
  inputBlur: function (e) {
    if(e.detail.value == ''){
      this.setData({
        focus: false
      })
    }
  },
  // 搜索
  changeInput:function(e){
    this.setData({
      seachValue:e.detail.value
    })
    var _this = this;
    if (e.detail.value != ''){
      wx.request({
        url: app.globalData.httpApiCar + 'shopInfo/all',
        data: {
          shopName: _this.data.seachValue,
          status: 2
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            _this.setData({
              actiItem: res.data.data
            })
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
      this.addHistory(this.data.seachValue);
    }
    else{
      if(this.data.search.businessType){
        this.getShopType(this.data.search.businessType)
      }
      else{
        if (this.data.search.isNew == 1){
          this.getbusinessnewList()
        }
        else{
          if (this.data.search.isHot == 1){
            this.getbusinesshotList()
          }
          else{
            this.getbusinessAllList()
          }
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})